package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<Setmeal> page = setmealDao.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.addSetmeal(setmeal);
        if (checkgroupIds.length > 0) {
            setmealDao.addSetmealAndCheckgroups(setmeal.getId(), checkgroupIds);
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> selectCheckgroupIdsBySetmealId(Integer id) {
        return setmealDao.selectCheckgroupIdsBySetmealId(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.deleteSetmealAndCheckgroupIds(setmeal.getId());
        setmealDao.update(setmeal);
        if (checkgroupIds.length > 0) {
            setmealDao.addSetmealAndCheckgroups(setmeal.getId(), checkgroupIds);
        }
    }

    @Override
    public void deleteSetmealItemById(Integer id) {
        setmealDao.deleteSetmealAndCheckgroupIds(id);
        setmealDao.deleteSetmeal(id);
    }
}
